package com.kuzufang.modules.main.roommate;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.transition.Explode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cunoraz.tagview.Tag;
import com.cunoraz.tagview.TagView;
import com.kuzufang.R;
import com.kuzufang.base.BaseActivity;
import com.kuzufang.modules.gaodemap.MapActivity;
import com.kuzufang.modules.imageutil.ImageHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by songyuanjin on 2017/9/17.
 */

public class RoommateDetailActivity extends BaseActivity {
    private Toolbar mToolBar;
    private ViewPager mImageViewPager;
    private RecyclerView mRoomConfigRecyclerView;
    private RoomConfigurationAdapter roomConfigAdapter;
    private RelativeLayout rlLocation;
    private TextView tvUserInfo;
    String url;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
            Explode explode = new Explode();
            getWindow().setExitTransition(explode);
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_roommate_detail);
        mToolBar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolBar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        mToolBar.setTitle("");

        url = getIntent().getStringExtra("room_detail");
        initViewPager();
        initTag();

        mRoomConfigRecyclerView = (RecyclerView) findViewById(R.id.room_recycler_view);
        roomConfigAdapter = new RoomConfigurationAdapter(this);
        mRoomConfigRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        mRoomConfigRecyclerView.setAdapter(roomConfigAdapter);
        mRoomConfigRecyclerView.setFocusable(false);

        rlLocation = (RelativeLayout) findViewById(R.id.rl_location);
        rlLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(RoommateDetailActivity.this, MapActivity.class));
            }
        });

        final CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            boolean isShow = false;
            int scrollRange = -1;

            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (scrollRange == -1) {
                    scrollRange = appBarLayout.getTotalScrollRange();
                }
                if (scrollRange + verticalOffset == 0) {
                    collapsingToolbarLayout.setTitle("玲珑湾");
                    isShow = true;
                } else if (isShow) {
                    collapsingToolbarLayout.setTitle(" ");//carefull there should a space between double quote otherwise it wont work
                    isShow = false;
                }
            }
        });

        tvUserInfo = (TextView) findViewById(R.id.tv_user_info);
        tvUserInfo.setText(Html.fromHtml(getString(R.string.room_user_info, "主卧", "不抽烟", "不晚睡", "可以养宠物") + "男女舍友都可以，最好学艺术的，干净一些，这样没事可以聊聊天，我经常进组指导演员，所以基本不怎么在家"));
    }

    private void initViewPager() {
        mImageViewPager = (ViewPager) findViewById(R.id.view_pager);
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabDots);
        tabLayout.setupWithViewPager(mImageViewPager, true);
        CustomPagerAdapter pagerAdapter = new CustomPagerAdapter(RoommateDetailActivity.this);
        mImageViewPager.setAdapter(pagerAdapter);
    }

    private void initTag() {
        TagView tagGroup = (TagView) findViewById(R.id.tag_room_group);
        List<Tag> tags = new ArrayList<>();
        List<String> stringList = new ArrayList<>();
        stringList.add("一室一厅");
        stringList.add("一厨一卫");
        stringList.add("朝南");
        stringList.add("整租");
        for (int i = 0; i < stringList.size(); i++) {
            Tag tag = new Tag(stringList.get(i));
            tag.layoutColor = Color.parseColor("#FFFFFF");
            tag.tagTextColor = Color.parseColor("#009688");
            tag.tagTextSize = 12.0f;
            tag.radius = 80.0f;
            tag.layoutBorderSize = 1;
            tag.layoutBorderColor = Color.parseColor("#009688");
            tags.add(tag);
        }
        tagGroup.addTags(tags);

        initTag2();
    }

    private void initTag2() {
        List<String> stringList = new ArrayList<>();
        stringList.add("运动");
        stringList.add("拒绝晚睡");
        stringList.add("禁止抽烟");
        final TagView tagGroup = (TagView) findViewById(R.id.tag_user_info_group);
        final List<Tag> tags = new ArrayList<>();
        for (int i = 0; i < stringList.size(); i++) {
            Tag tag = new Tag(stringList.get(i));
            tag.layoutColor = Color.parseColor("#FFFFFF");
            tag.tagTextColor = Color.parseColor("#009688");
            tag.tagTextSize = 12.0f;
            tag.radius = 80.0f;
            tag.layoutBorderSize = 1;
            tag.layoutBorderColor = Color.parseColor("#009688");
            tags.add(tag);
        }
        tagGroup.addTags(tags);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            case R.id.share:
                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                String shareBodyText = "Check it out. Your message goes here";
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject here");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBodyText);
                startActivity(Intent.createChooser(sharingIntent, "Shearing Option"));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    class CustomPagerAdapter extends PagerAdapter {
        Context mContext;
        LayoutInflater mLayoutInflater;

        public CustomPagerAdapter(Context context) {
            mContext = context;
            mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return 6;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == ((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View itemView = mLayoutInflater.inflate(R.layout.roommate_pager_item, container, false);
            ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
            ImageHandler.getImageLoader().load(RoommateDetailActivity.this, url, imageView);
            container.addView(itemView);
            return itemView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((LinearLayout) object);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.detail_right_menu, menu);
        return true;
    }


}
